<!DOCTYPE html>
<html>

<head>
	<title></title>
	<meta charset="UTF-8">
	<link rel="stylesheet" href="../../element-ui-2.13.0/lib/theme-chalk/index.css" />
	<script type="text/javascript" src="../../vue/vue-v2.6.10.js"></script>
	<script type="text/javascript" src="../../element-ui-2.13.0/lib/index.js"></script>
	<script type="text/javascript" src="../../vue/axios-0.18.0.js"></script>
</head>

<body>
	<div id="app">
		<!--上传课程-->
		<el-tag type="info" effect="dark" closable="true" :disable-transitions="disable" @close="handleClose">添加课程
		</el-tag>
		<div v-if="inputVisible">
			<!--表单-->
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<el-form-item label="课程名称" prop="courseName">
					<el-input v-model="ruleForm.courseName" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="课程简介" prop="descs">
					<el-input v-model="ruleForm.descs" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="课程类型" prop="courseType">
					<el-select v-model="ruleForm.courseType" placeholder="请选择课程类型">
						<el-option label="JavaEE" value="1"></el-option>
						<el-option label="数据库" value="2"></el-option>
						<el-option label="前端技术" value="3"></el-option>
					</el-select>
				</el-form-item>

				<el-upload style="margin-left: 25px;" :on-remove="handleRemove1" :on-exceed="handleExceed"
					:http-request="myUpload" class="upload-demo" ref="upload1" :data="{value:1}"
					action="http://192.168.194.233/education/courses?method=uploadFile" :limit="1" :auto-upload="false">
					<el-button slot="trigger" size="small" type="primary">选取图片</el-button>
					<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload(1)">上传到服务器
					</el-button>
				</el-upload>
				<img :src="dialogImageUrl" style="width: 100px;height: 100px;margin-left: 25px;" alt="" />

				<el-upload style="margin-left: 25px;" :on-remove="handleRemove2" :on-exceed="handleExceed"
					:http-request="myUpload" class="upload-demo" ref="upload2" :data="{value:2}"
					action="http://192.168.194.233/education/courses?method=uploadFile" :limit="1" :auto-upload="false">
					<el-button slot="trigger" size="small" type="primary">选取视频</el-button>
					<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload(2)">上传到服务器
					</el-button>
				</el-upload>

				<video controls autoplay muted="muted"
					style="width: 100px;height: 100px;margin-left: 25px;margin-top: 5px;" :src="dialogVedioUrl">
				</video>

				<el-form-item label="课程价格" prop="coursePrice">
					<el-input v-model="ruleForm.coursePrice" style="width: 210px;"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="ruleForm.status">
						<el-radio label="0">未上架</el-radio>
						<el-radio label="1">上架</el-radio>
						<el-radio label="2">下架</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" style="margin-left: 20px;">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" @click="submitForm()">确 定</el-button>
			</div>
		</div>
		<!--结尾-->
	</div>
</body>
<script>
	axios.defaults.withCredentials = true
	new Vue({
		el: "#app",
		data: {
			/*校验的字段*/
			ruleForm: {
				courseName: '',
				descs: '',
				courseType: '',
				coursePrice: '',
				courseImage: '',
				courseVideo: '',
				status: ''
			},
			/*规则*/
			rules: {
				courseName: [{
					required: true,
					message: '请输入课程名称',
					trigger: 'blur'
				},],
				descs: [{
					required: true,
					message: '请选择课程简介',
					trigger: 'blur'
				}],
				courseType: [{
					required: true,
					message: '请选择课程类型',
					trigger: 'change'
				}],
				coursePrice: [{
					required: true,
					message: '请输入课程价格',
					trigger: 'blur'
				}],
				status: [{
					required: true,
					message: '请至少选择一个状态',
					trigger: 'change'

				}]
			},
			/*文件上传*/
			dialogImageUrl: '',
			dialogVedioUrl: '',
			/*选项卡*/
			inputVisible: true,
			disable: false,
		},
		methods: {
			submitUpload(value) {
				if (value == 1) {
					this.$refs.upload1.submit();
				} else {
					this.$refs.upload2.submit();
				}
			},

			myUpload(content) {
				var f = new FormData();
				f.append("file", content.file);
				axios({
					url: "http://192.168.194.233:8080/edu/course?flag=upload",
					method: 'post',
					data: f,
					params: {
						value: content.data.value
					}
				}).then(res => {
					if (content.data.value == 1) {
						this.$message.success("上传图片成功！");
						this.dialogImageUrl = "http://192.168.194.233:8080/cs_res/course/pic/" + res.data;
						this.ruleForm.courseImage = res.data;
					} else {
						this.$message.success("上传视频成功！");
						this.dialogVedioUrl = "http://192.168.194.233:8080/cs_res/course/video/" + res.data;
						this.ruleForm.courseVideo = res.data;
					}
				});
			},

			handleRemove1(file, fileList) {
				axios.get("http://192.168.194.233:8080/edu/course?flag=remove&value=1&filename=" + this.ruleForm.courseImage).then(res => {
					this.$message.success("取消图片上传！");
					this.ruleForm.courseImage = '';
					this.dialogImageUrl = '';
				})
			},
			handleRemove2(file, fileList) {

				axios.get("http://192.168.194.233:8080/edu/course?flag=remove&value=2&filename=" + this.ruleForm.courseVideo).then(res => {
					this.$message.success("取消视频上传！");
					this.ruleForm.courseVideo = '';
					this.dialogVedioUrl = '';
				});

			},

			handleClose() {

			},

			handleExceed(files, fileList) {
				this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
			},


			submitForm() {
				this.$refs.ruleForm.validate(res => {
					if (res) {
						axios({
							method: "post",
							url: 'http://192.168.194.233:8080/edu/course',
							data: this.ruleForm,
							params: {
								flag: 'add'
							}
						}).then(res => {
							this.$message.success(res.data);
							setTimeout("location.href='index.html'", 1000);
						});
					} else {
						this.$message.error("请认真填写表单！");
					}
				});
			}

		}
	})
</script>

</html>